import metchonServices from '@/services/metchonService.js';
import file from '@/common/utils/file.js'

export default {
	data() {
		return {
			images: [],
			fromData: {},
			sblxOptions: [],
			schoolOptions: [],
			schoolValue: null,
			xyOptions: [],
			xtyValue: null,
			sysOptions: []
		}
	},
	onShow() {
		console.log('tianjia show1')
	},
	async onLoad() {
		await this.$store.dispatch('checkLoginAction');
		this.schoolOptions = await metchonServices.getInstitutionList('');
		this.sblxOptions = await metchonServices.getTypesList();
		this.$eventBus.$on('tabbarEvent', (data) => {
		      console.log('tianjia 111'); // 输出{ index: xxx }
		      // 处理tabbar事件的相关操作
		   })
	},
	onTabItemTap : function(e) {
		this.init();
	},
	methods: {
		init() {
			this.fromData = {
				bxrqend: "",
				bxrqstart: "",
				createBy: "",
				createTime: "",
				gmrq: "",
				id: "",
				orgcode: "",
				sbbh: "",
				sbewm: "",
				sblx: "",
				sbmc: "",
				sysOrgCode: "",
				tpurl: "",
				updateBy: "",
				updateTime: "",
				yl: "",
				yl1: "",
				yl2: ""
			};
			this.schoolValue = '';
			this.xtyValue = '';
			this.images = [];
		},
		async onChangeSchool(value) {
			this.xyOptions = await metchonServices.getInstitutionList(value);
		},
		async onChangexy(value) {
			this.sysOptions = await metchonServices.getInstitutionList(value);
		},
		onSaoMiao() {
			const that = this;
			// that.fromData.sbewm = "123dd";
			// 调起条码扫描
			uni.scanCode({
				onlyFromCamera: false, // 是否只能从相机扫码，不允许从相册选择图片
				scanType: ['qrCode'], // 扫码类型 qrCode二维码
				autoZoom: true, // 是否启用自动放大，默认启用
				success: function(res) {
					console.log(`扫码结果：${res.result}`);
					that.fromData.sbewm = res.result;
					
				},
				fail: function(err) {
					console.log(`错误：${err}`);
				},
				complete: function(data) {}
			});
		},
		// 获取上传状态
		async select(obj) {
			const tempFilePaths = obj.tempFilePaths;
			var img = await file.uploadFile(tempFilePaths[0]);
			this.images.push(img);
		},
		onDeleteFile(obj) {
			console.log('this.images',this.images)
			console.log('obj.tempFilePath',obj.tempFilePath)
			this.images = this.images.filter(e => e.url !== obj.tempFilePath);
			console.log('this.images2',this.images)
		},
		async onSubmit() {
			this.fromData.yl = this.schoolOptions.find(e => e.ID === this.schoolValue).depart_name;
			this.fromData.yl1 = this.xyOptions.find(e => e.ID === this.xtyValue).depart_name;
			this.fromData.yl2 = this.sysOptions.find(e => e.ID === this.fromData.orgcode).depart_name;
			console.log(this.fromData)
			if (!this.fromData.orgcode) {
				this.$message.error('请选择实验室');
				return;
			}

			if (!this.fromData.sblx) {
				this.$message.error('请选择设备类型');
				return;
			}

			if (!this.fromData.sbbh) {
				this.$message.error('请输入设备编号');
				return;
			}

			if (!this.fromData.sbmc) {
				this.$message.error('请输入设备名称');
				return;
			}

			if (!this.fromData.sbewm) {
				this.$message.error('请绑定二维码');
				return;
			}

			if (!this.images || this.images.length === 0) {
				this.$message.error('请上传图片');
				return;
			}
			debugger;
			
			this.fromData.tpurl = this.images[0].name;
			await metchonServices.add(this.fromData);
			this.$message.success('添加成功');
		}
	}
}